package com.ayoka.parenting.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.CommentBean;
import com.jincp.common.utils.SpannableClickable;
import com.jincp.common.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yiwei on 16/7/9.
 */
public class CommentListView extends LinearLayout {
    private int itemColor;
    private int itemSelectorColor;
    private OnItemClickListener onItemClickListener;
    private List<CommentBean> commentBeanList;
    private LayoutInflater layoutInflater ;

    private boolean isClickable;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setDatas(List<CommentBean> commentBeanList, boolean isClickable){
        if(commentBeanList == null ){
            commentBeanList = new ArrayList<>();
        }
        this.commentBeanList = commentBeanList;
        this.isClickable = isClickable;
        notifyDataSetChanged();
    }

    public List<CommentBean> getDatas(){
        return commentBeanList;
    }

    public CommentListView(Context context) {
        super(context);
    }

    public CommentListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
    }

    public CommentListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
    }

    protected void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.CommentListView, 0, 0);
        try {
            //textview的默认颜色
            itemColor = typedArray.getColor(R.styleable.CommentListView_item_color, getResources().getColor(R.color.comment_item_default));
            itemSelectorColor = typedArray.getColor(R.styleable.CommentListView_item_selector_color, getResources().getColor(R.color.comment_item_selector));

        }finally {
            typedArray.recycle();
        }
    }

    public void notifyDataSetChanged(){

        removeAllViews();
        if(commentBeanList == null || commentBeanList.size() == 0){
            return;
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for(int i = 0; i< commentBeanList.size(); i++){
            final int index = i;
            View view = getView(index);
            if(view == null){
                throw new NullPointerException("listview item layout is null, please check getView()...");
            }

            addView(view, index, layoutParams);
        }

    }

    private View getView(final int position){
        if(layoutInflater == null){
            layoutInflater = LayoutInflater.from(getContext());
        }
        View convertView = layoutInflater.inflate(R.layout.layout_topic_comment_list, null, false);

        TextView commentTv = (TextView) convertView.findViewById(R.id.commentTv);

        final CommentBean bean = commentBeanList.get(position);
        String name = bean.UserInfo.WeixinNickName;
        String userId = bean.UserInfo.ID;
        String toReplyName = "";
        if (bean.ReplyToUserInfo != null) {
            toReplyName = bean.ReplyToUserInfo.WeixinNickName;
        }

        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(setClickableSpan(name, String.valueOf(bean.UserInfo.ID)));

        if (!TextUtils.isEmpty(toReplyName)) {

            builder.append(" 回复 ");
            builder.append(setClickableSpan(toReplyName, String.valueOf(bean.ReplyToUserInfo.ID)));
        }
        builder.append(": ");
        //转换表情字符
        String contentBodyStr = bean.Content;
        builder.append(StringUtil.formatUrlString(contentBodyStr));
        commentTv.setText(builder);
        if (isClickable) {
        final CircleMovementMethod circleMovementMethod = new CircleMovementMethod(itemSelectorColor, itemSelectorColor);
            commentTv.setMovementMethod(circleMovementMethod);
            commentTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isClickable && circleMovementMethod.isPassToTv()) {
                        if(onItemClickListener!=null){
                            onItemClickListener.onItemClick(position);
                        }
                    }
                }
            });
            commentTv.setOnLongClickListener(new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (isClickable && circleMovementMethod.isPassToTv()) {
                        if(onItemClickListener!=null){
                            onItemClickListener.onItemLongClick(position);
                        }
                        return true;
                    }
                    return false;
                }
            });
        }
        return convertView;
    }

    @NonNull
    private SpannableString setClickableSpan(final String name, final String id) {
        SpannableString subjectSpanText = new SpannableString(name);
        subjectSpanText.setSpan(new SpannableClickable(itemColor){
                                    @Override
                                    public void onClick(View widget) {
                                        if(isClickable && onItemClickListener!=null){
                                            onItemClickListener.onItemUserClick(name, id);
                                        }
                                    }
                                }, 0, subjectSpanText.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return subjectSpanText;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);

        void onItemLongClick(int position);

        void onItemUserClick(String useName, String userId);
    }
}
